package com.qyc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qyc.enums.CodeMsgEnum;
import com.qyc.exception.SystemException;
import com.qyc.pojo.Account;
import com.qyc.service.AccountService;
import com.qyc.mapper.AccountMapper;
import com.qyc.utils.JwtUtil;
import com.qyc.utils.ResponseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【base_account(系统用户)】的数据库操作Service实现
* @createDate 2023-06-20 16:29:45
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService{

    //注入用户Mapper层对象
    @Autowired
    private AccountMapper accountMapper;

    /**
     * @Author qinyc
     * @Description  登录接口
     * @version: v1.0
     * @Date 19:17 2023/6/20
     **/
    @Override
    public ResponseResult login(String username, String password) {
        /*
         * 参数校验
         */

        //判断参数是否为空
        if (!StringUtils.hasText(username)){
            //用户名为空
            throw new SystemException(CodeMsgEnum.USERNAME_IS_NOT_NULL);
        }
        if (!StringUtils.hasText(password)){
            //密码为空
            throw new SystemException(CodeMsgEnum.PASSWORD_IS_NOT_NULL);
        }
        //创建条件选择器
        LambdaQueryWrapper<Account> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据用户名查询
        accountLambdaQueryWrapper.eq(Account::getUsername,username);
        //根据密码查询
        accountLambdaQueryWrapper.eq(Account::getPassword,password);
        //根据创建的条件进行查询
        Account account = accountMapper.selectOne(accountLambdaQueryWrapper);
        //判断查询出的用户对象是否为空
        if (Objects.isNull(account)){
            //查询出来的对象为空(用户名或密码错误)
            throw new SystemException(CodeMsgEnum.USERNAME_OR_PASSWORD_ERROR);
        }

        /*
         * 参数校验通过
         */
        //根据用户ID生成Token
        String jwt = JwtUtil.createJWT(account.getAccountId());
        //创建Map集合存放token
        Map<String,String> token = new HashMap<>();
        token.put("token",jwt);
        //返回成功响应信息
        return ResponseResult.success(token);
    }

    /**
     * @Author qinyc
     * @Description  获取当前用户信息
     * @version: v1.0
     * @Date 1:33 2023/6/22
     **/
    @Override
    public ResponseResult getAccountInfo(String token) {
        //初始化用户对象
        Account account = null;
        try {
            //解析Token
            Claims claims = JwtUtil.parseJWT(token);
            //拿到用户ID
            String accountId = (String) claims.get("sub");
            //根据用户ID查询用户信息
            account = accountMapper.selectById(accountId);
        } catch (Exception e) {
            //Token不合法
            throw new SystemException(CodeMsgEnum.LOGIN_IS_EXPIRED);
        }
        //返回统一响应对象
        return ResponseResult.success(account);
    }
}




